package com.joplus.housekeeper.services;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.hyphenate.chatuidemo.DemoApplication;
import com.hyphenate.chatuidemo.ui.SplashActivity;
import com.joplus.housekeeper.bean.LoginResult;
import com.joplus.housekeeper.bean.XmppUser;
import com.joplus.housekeeper.ui.activity.MainActivity;
import com.joplus.housekeeper.utils.SPUtils;
import com.joplus.housekeeper.utils.T;
import com.joplus.housekeeper.xmpp.SmackManager;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author： ccm
 * time： 2017/7/11.
 * description：
 */

public class XmppService extends Service {


    private String userName;
    private String userPassword;
    private Map<String, String> attributes;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private SmackManager instance;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {


            Bundle extras = intent.getExtras();

            userName = extras.getString("userName");
            userPassword = extras.getString("password");


            attributes = new HashMap<>();
            attributes.put("name", userName);
            //     instance.registerUser(userName, "123456", attributes);
            login();
        }
        return super.onStartCommand(intent, flags, startId);


    }

    private void login() {
        attributes.put("name", userName);
        Observable.create(new ObservableOnSubscribe<SmackManager>() {
            @Override
            public void subscribe(ObservableEmitter<SmackManager> e) throws Exception {
                instance = SmackManager.getInstance();
                instance.getConnection();
                e.onNext(instance);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<SmackManager>() {
            @Override
            public void accept(SmackManager smackManager) throws Exception {

                LoginResult login = instance.login(userName, userPassword);
                if (login.isSuccess()) {

                    SPUtils.put(XmppService.this, "im", userName);
                } else {
                    login();
                }
            }
        });
       /* Observable.create(new ObservableOnSubscribe<SmackManager>() {
            @Override
            public void subscribe(ObservableEmitter<SmackManager> e) throws Exception {
                instance = SmackManager.getInstance();
                instance.getConnection();
                e.onNext(instance);
            }
        }).subscribeOn(Schedulers.io()).map(new Function<SmackManager, Boolean>() {
            @Override
            public Boolean apply(SmackManager o) throws Exception {
                boolean b = o.registerUser(userName, userPassword, attributes);

                return b;
            }
        }).observeOn(Schedulers.io()).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object aBoolean) throws Exception {
                LoginResult login = instance.login(userName, userPassword);

                instance.addMessageListener();
                instance.addFileListener();
                instance.offlineMessage(instance.getConnection());


            }
        });*/


    }

}
